package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Time;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 文件上传功能
     */
    @Override
    public String upload(MultipartFile multipartFile) throws IOException {
        //原始文件名
        String originalFileName = multipartFile.getOriginalFilename();
        //使用UUID构造不重复的文件名
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFileName;
        //获取输入流并上传
        InputStream is = multipartFile.getInputStream();
        //调用七牛云工具类上传文件
        qiniuUtils.upload2Qiniu(is, fileName);
        //构造返回值  返回的是七牛云图片地址
        //xxx.com/文件名
        String pic = qiniuUtils.getUrlPrefix() + fileName;
        //打印图片信息
        log.info("存储到Redis中的临时文件名称 ===> {}", fileName);
        //存放两个图片标识在Redis中
        redisTemplate.opsForSet().add(RedisConst.SETMEAL_PIC_RESOURCES, fileName);
        //设置存活时间60s
        BoundSetOperations setOps = redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_DB_RESOURCES);
        setOps.add(fileName);
        setOps.expire(60, TimeUnit.SECONDS);
        //返回图片标识
        return pic;
    }

    @Override
    @Transactional
    public void saveSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}", setmeal, checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);

        log.info("存储到数据库中的文件名称 ===> {}", setmeal.getImg());

        //图片完成持久化存储后，删除Redis中的缓存
        redisTemplate.opsForSet().remove(RedisConst.SETMEAL_PIC_RESOURCES,setmeal.getImg());
        redisTemplate.opsForSet().remove(RedisConst.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

        //删除临时图片标识，证明图片已经持久化存储
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }



    /**
     * 查询套餐预约占比数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealReport() {
        return setMealDao.findSetmealCount();
    }

    /*
     *根据id获取套餐信息
     * */
    @Override
    public Setmeal getSetmeal(Integer id) {
        return setMealDao.selectOneSetmeal(id);
    }
    /**
     * 查询套餐展示列表
     * @return
     */

    @Override
    public List<Setmeal> findSetmealList() {

        return setMealDao.selectSetMealList();
    }

    /*
     * 通过套餐id查询所有检查组信息
     * */
    @Override
    public List<CheckGroup> findCheckGroupIdsBySetmealId(Integer id) {
        List<CheckGroup> checkGroups = setMealDao.findCheckGroupIdsBySetmealId(id);
        return checkGroups;
    }


}
